/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Chat;

import javax.microedition.amms.control.tuner.TunerControl;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

/**
 *
 * @author Mr Bao
 */
public class LoginForm extends Canvas {

    private String strUsername = "Tài khoản";
    private String strPassword = "Mật khẩu";
    private String strInvisible = "Ẩn nick";
    private String strSave = "Lưu tài khoản";
    private String strLogin = "Đăng nhập";
    private int select;
    private boolean isShowPointer;
    private String title = "Đăng nhập";
    private String username;
    private String password;
    private int pointer;
    private Image iconCheck;
    private Image iconUnCheck;
    private boolean isInvisible;
    private boolean isSave;

    protected void paint(Graphics g) {
        setFullScreenMode(true);
        Util.ClearScreen(g, getWidth(), getHeight());
        Util.DrawTitleBar(g, "", title, "", getWidth(), getHeight());
    }

    private void DrawUserName(Graphics g, int y) {
        int delta = getWidth() / 12;
        g.setColor(0);
        g.setFont(Util.FontSmall);
        g.drawString(strUsername, delta, y, 0);
        y += Util.FontSmall.getHeight();
        g.setColor(60);
        g.drawRoundRect(delta, y, getWidth() - delta * 2, Util.FontSmall.getHeight() + 5, 6, 6);
        g.setColor(200, 200, 200);
        g.drawRoundRect(delta + 1, y + 1, getWidth() - delta * 2 - 1, Util.FontSmall.getHeight() + 4, 6, 6);
        g.setColor(0xffffff);
        g.fillRoundRect(delta + 1, y + 1, getWidth() - delta * 2 - 2, Util.FontSmall.getHeight() + 3, 6, 6);
        if (select == 1 && isShowPointer) {
            g.setColor(0);
            if (Util.FontSmall.substringWidth(username, 0, pointer) > (getWidth() - delta * 2 - 1)) {
                g.drawLine(getWidth() - delta - 2, y + 2, getWidth() - delta - 2, y + Util.FontSmall.getHeight() + 2);
            } else {
                g.drawLine(delta + 2 + Util.FontSmall.substringWidth(username, 0, pointer), y + 2, delta + 2 + Util.FontSmall.substringWidth(username, 0, pointer), y + Util.FontSmall.getHeight() + 2);
            }
        }
        g.setColor(0);
        if (Util.FontSmall.stringWidth(username) > getWidth() - (delta * 2) - 1) {
            String str = "";
            int i;
            if (Util.FontSmall.substringWidth(username, 0, pointer) > getWidth() - (delta * 2) - 1) {
                i = 0;
                while (true) {
                    if (i >= 0 && i < username.length()) {
                        str = username.substring(i, pointer);
                        if (Util.FontSmall.stringWidth(str) <= getWidth() - (delta * 2) - 1) {
                            break;
                        } else {
                            i++;
                        }
                    } else {
                        break;
                    }
                }
                if (!str.equalsIgnoreCase("")) {
                    g.drawString(str, getWidth() - delta - 2, y + 3, Graphics.RIGHT | Graphics.TOP);
                }
            } else {
                i = username.length() - 1;
                while (true) {
                    if (i >= 0 && i < username.length()) {
                        str = username.substring(0, i);
                        if (Util.FontSmall.stringWidth(str) <= getWidth() - (delta * 2) - 1) {
                            break;
                        }
                        i--;
                    } else {
                        break;
                    }

                }
                if (!str.equalsIgnoreCase("")) {
                    g.drawString(str, delta + 2, y + 3, 0);
                }
            }
        } else {
            g.drawString(username, delta + 2, y + 3, 0);
        }
    }

    private void DrawPassword(Graphics g, int y) {
        int delta = getWidth() / 12;
        g.drawString(strPassword, delta, y, 0);
        y += Util.FontSmall.getHeight();
        g.setColor(60);
        g.drawRoundRect(delta, y, getWidth() - delta * 2, Util.FontSmall.getHeight() + 5, 6, 6);
        g.setColor(200, 200, 200);
        g.drawRoundRect(delta + 1, y + 1, getWidth() - delta * 2 - 1, Util.FontSmall.getHeight() + 4, 6, 6);
        g.setColor(0xffffff);
        g.fillRoundRect(delta + 1, y + 1, getWidth() - delta * 2 - 2, Util.FontSmall.getHeight() + 3, 6, 6);
        if (select == 3 && isShowPointer) {
            g.setColor(0);
            if (Util.FontSmall.substringWidth(password, 0, pointer) > getWidth() - delta * 2 - 1) {
                g.drawLine(getWidth() - delta - 2, y + 2, getWidth() - delta - 2, y + Util.FontSmall.getHeight() + 2);
            } else {
                g.drawLine(delta + 2 + Util.FontSmall.substringWidth(password, 0, pointer), y + 2, delta + 2 + Util.FontSmall.substringWidth(password, 0, pointer), y + Util.FontSmall.getHeight() + 2);
            }
        }
        g.setColor(0);
        if (Util.FontSmall.stringWidth(password) > getWidth() - (delta * 2) - 1) {
            String str = "";
            int i;
            if (Util.FontSmall.substringWidth(password, 0, pointer) > getWidth() - (delta * 2) - 1) {
                i = 0;
                while (true) {
                    if (i >= 0 && i < password.length()) {
                        str = password.substring(i, pointer);
                        if (Util.FontSmall.stringWidth(str) <= getWidth() - (delta * 2) - 1) {
                            break;
                        } else {
                            i++;
                        }
                    } else {
                        break;
                    }
                }
                if (!str.equalsIgnoreCase("")) {
                    g.drawString(str, getWidth() - delta - 2, y + 3, Graphics.RIGHT | Graphics.TOP);
                }
            } else {
                i = password.length() - 1;
                while (true) {
                    if (i >= 0 && i < password.length()) {
                        str = password.substring(0, i);
                        if (Util.FontSmall.stringWidth(str) <= getWidth() - (delta * 2) - 1) {
                            break;
                        }
                        i--;
                    } else {
                        break;
                    }
                }
                if (!str.equalsIgnoreCase("")) {
                    g.drawString(str, delta + 2, y + 3, 0);
                }
            }
        } else {
            g.drawString(password, delta + 2, y + 3, 0);
        }
    }

    private void DrawCheckboxInvisible(Graphics g, int y) {
        int delta = getWidth() / 12;
        g.setColor(Util.color2);
        int max = Util.FontSmall.getHeight() > iconCheck.getHeight() ? Util.FontSmall.getHeight() : iconCheck.getHeight();
        if (select == 3) {
            g.drawRoundRect(delta - 1, y - 1, Util.FontSmall.stringWidth(strInvisible) + 2 + iconCheck.getWidth() + 1, max + 3, 6, 6);
        }
        g.setColor(0);
        if (isInvisible) {
            g.drawImage(iconCheck, delta, y + (max - iconCheck.getHeight()) / 2 + 1, 0);
        } else {
            g.drawImage(iconUnCheck, delta, y + (max - iconUnCheck.getHeight()) / 2 + 1, 0);
        }
        g.drawString(strInvisible, delta + iconCheck.getWidth() + 2, y + (max - Util.FontSmall.getHeight()) / 2 + 1, 0);
    }

    private void DrawCheckBoxSave(Graphics g, int y) {
        int delta = getWidth() / 12;
        g.setColor(Util.color2);
        int max = Util.FontSmall.getHeight() > iconCheck.getHeight() ? Util.FontSmall.getHeight() : iconCheck.getHeight();
        if (select == 4) {
            g.drawRoundRect(delta - 1, y - 1, Util.FontSmall.stringWidth(strSave) + 2 + iconCheck.getWidth() + 1, max + 3, 6, 6);
        }
        g.setColor(0);
        if (isSave) {
            g.drawImage(iconCheck, delta, y + (max - iconCheck.getHeight()) / 2 + 1, 0);
        } else {
            g.drawImage(iconUnCheck, delta, y + (max - iconUnCheck.getHeight()) / 2 + 1, 0);
        }
        g.drawString(strSave, delta + iconCheck.getWidth() + 2, y + (max - Util.FontSmall.getHeight()) / 2 + 1, 0);
    }

    private void DrawButton(Graphics g, int y) {
        if (select == 5) {
            Util.GradientBox(g, Util.color1, Util.color2, (getWidth() - Util.FontSmall.stringWidth(strLogin)) / 2, y, Util.FontSmall.stringWidth(strLogin) + 10, (Util.FontSmall.getHeight() + 10) / 2 + 1, 0);
            g.setColor(Util.color3);
            g.fillRect((getWidth() - Util.FontSmall.stringWidth(strLogin) - 10) / 2, y + (Util.FontSmall.getHeight() + 10) / 2, Util.FontSmall.stringWidth(strLogin) + 10, (Util.FontSmall.getHeight() + 10) / 2);
            g.setColor(Util.color2);
        } else {
            Util.GradientBox(g, Util.color1, Util.color2 - 50, (getWidth() - Util.FontSmall.stringWidth(strLogin) - 10) / 2, y, Util.FontSmall.stringWidth(strLogin) + 10, (Util.FontSmall.getHeight() + 10) / 2 + 1, 0);
            g.setColor(Util.color3);
            g.fillRect((getWidth() - Util.FontSmall.stringWidth(strLogin) - 10) / 2, y + (Util.FontSmall.getHeight() + 10) / 2, Util.FontSmall.stringWidth(strLogin) + 10, (Util.FontSmall.getHeight() + 10) / 2);
            g.setColor(150, 150, 150);
        }
        g.drawRoundRect((getWidth() - Util.FontSmall.stringWidth(strLogin) - 14) / 2, y - 1, Util.FontSmall.stringWidth(strLogin) + 13, Util.FontSmall.getHeight() + 12, 6, 6);
        g.setColor(210, 210, 210);
        g.drawString(strLogin, (getWidth() - Util.FontSmall.stringWidth(strLogin)) / 2, y + 5, 0);
    }
}
